const MongoClient = require('mongodb').MongoClient;
const connectStr = 'mongodb://localhost:27017';
// function getNextSequenceValue(sequenceName, collection){
//    var sequenceDocument = collection.findAndModify(
//       {
//          query:{_id: sequenceName },
//          update: {$inc:{sequence_value:1}},
//          "new":true
//       });
//    return sequenceDocument.sequence_value;
// }
const mysql = {
  insert (collectionName, insertData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.insert(insertData, (err) => {
          if (err) {
            reject(err)
            client.close()
          } else {
            resolve()
            client.close()
          }
        })
      })
    })
  },
  remove (collectionName, deleteData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.remove(deleteData, (err) => {
          if (err) {
            reject(err)
            client.close()
          } else {
            resolve()
            client.close()
          }
        })
      })
    })
  },
  updateOne (collectionName, whereData, updateData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.updateOne(whereData, updateData, (err) => {
          if (err) {
            reject(err)
            client.close()
          } else {
            resolve()
            client.close()
          }
        })
      })
    })
  },
  updateMany (collectionName, whereData, updateData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.updateMany(whereData, updateData, (err) => {
          if (err) {
            reject(err)
            client.close()
          } else {
            resolve()
            client.close()
          }
        })
      })
    })
  },
  find (collectionName, whereData, showData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.find(whereData, showData).toArray((err, data) => {
          if (err) {
            console.log('1')
            reject(err);
            client.close();
          } else {
            console.log(data)
            resolve(data);
            client.close();
          }
        })
      })
    })
  },
  sort (collectionName, whereData, showData, sortData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.find(whereData, showData).sort(sortData).toArray((err, data) => {
          if (err) {
            reject(err);
            client.close();
          } else {
            resolve(data);
            client.close();
          }
        })
      })
    })
  },
  distinct (collectionName, type) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        // console.log(collection.distinct(type))
        const arr = collection.distinct(type)
        resolve(arr)
        client.close()
      })
    })
  },
  paging (collectionName, pageCode, limitNum, sortData) {
    return new Promise((resolve, reject) => {
      MongoClient.connect(connectStr, (err, client) => {
        if (err) throw err;
        const db = client.db('boke');
        const collection = db.collection(collectionName);
        collection.find({}, {}).limit(limitNum).skip((pageCode - 1) * limitNum).sort(sortData).toArray((err, data) => {
          if (err) {
            reject(err)
          } else {
            resolve(data)
          }
          client.close();
        })
      })
    })
  }
}

module.exports = mysql;
